var number = 0, number2 = 0, number3 = 10;
$(function () {
    var cApi = new common, bbJson = cApi.getJsonObj(sessionStorage.getItem("bmJson"));
    if (bbJson) {
        var jsonName = bbJson.FOLLOW_PEOPLE_NAMES, arrayJsonName = jsonName.split(",");
        console.log(arrayJsonName.length), console.log(jsonName);
        for (var i = 1; i < arrayJsonName.length; i++) {
            number3 += 1, number = arrayJsonName.length;
            var strr = '<li class="mui-table-view-cell li"><div class="mui-input-row join-ball"><label class="ball">姓名</label><input type="text" name="name' + number3 + '" class="join-float back-add" value="' + arrayJsonName[i] + '" placeholder="需与身份证上名字一致" required maxlength="24"><div class="flex-item vCenter reduce-btn"><span class="icon-delete fun-ico"></span></div></div></li>';
            $(".people-list").append(strr)
        }
    }
    mui("body").on("tap", ".icon-delete", function () {
        $(this).parents(".li").remove(), number -= 1
    }), mui("body").on("tap", ".add-people", function () {
        if ($("html,body").animate({scrollTop: $(document).height()}, 100), number < 10) {
            number += 1, number2 += 1, number3 += 1;
            var e = $(".people-list");
            e.append('<li class="mui-table-view-cell li"><div class="mui-input-row join-ball"><label class="ball">姓名</label><input type="text" name="name' + number2 + '" class="mui-input-clear join-float" placeholder="需与身份证上名字一致" required maxlength="24"><div class="flex-item vCenter reduce-btn"><span class="icon-delete fun-ico"></span></div></div></li>')
        } else mui.toast("已达添加人数上限")
    });
    var submitOrder = function (e) {
        1 == e.code ? (console.log(e), setTimeout(function () {
            mui.openWindow({
                id: "activity-put.html",
                url: "activity-put.html",
                styles: {popGesture: "close"},
                show: {aniShow: "pop-in"},
                waiting: {autoShow: !0}
            })
        }, 100)) : mui.toast(e.message)
    };
    $("#inputForm").html5Validate(function () {
        var bmJson = {}, json = cApi.strToObj(decodeURIComponent($(this).serialize(), !0));
        if (json) {
            for (var result = "", i = 1; i <= number3; i++)result = result + eval("json.name" + i) + ",";
            bmnameJson = json.NAMES + "," + result;
            var arrayName = bmnameJson.split(",");
            console.log(arrayName);
            for (var r = 0; r < arrayName.length; r++)"" != arrayName[r] && "undefined" != arrayName[r] || (arrayName.splice(r, 1), r -= 1);
            stringName = arrayName.join(","), console.log(stringName), bmJson.MARKETINGACTIVITY_ID = MARKETINGACTIVITY_ID, bmJson.NAME = json.NAME, bmJson.MOBILE = json.MOBILE, bmJson.FOLLOW_PEOPLE_NAMES = stringName, bmJson.FOLLOW_PEOPLE_MOBILE = json.MOBILES, sessionStorage.setItem("bmJson", JSON.stringify(bmJson));
            var myData = $.extend({}, bmJson, {FKEY: cApi.getKey("applyActivity")});
            myData.personArray && (myData.personArray = JSON.stringify(myData.personArray)), console.log(myData);
            var hdJson = {
                url: cApi.hostUrl + "mobileappmarketing/applyActivity.do",
                data: myData,
                success: submitOrder
            };
            hdJson = $.extend({}, cApi.ajaxOption, hdJson), $.ajax(hdJson)
        } else mui.toast("转化异常!")
    });
    var showDetails = function (e) {
        if (1 == e.code) {
            console.log(e);
            var a = e.activityDetail;
            $(".activity-name").text(a.TITLE);
            var t = new Date(a.APPLY_BEGIN_TIME);
            $(".activity-date").text(t.Format("yyyy.MM.dd"))
        } else mui.toast(e.message)
    }, MARKETINGACTIVITY_ID = cApi.getQueryString("MARKETINGACTIVITY_ID"), activityJson = {
        url: cApi.hostUrl + "mobileappmarketing/activityDetail.do",
        data: {MARKETINGACTIVITY_ID: MARKETINGACTIVITY_ID, FKEY: cApi.getKey("activityDetail")},
        success: showDetails
    };
    activityJson = $.extend({}, cApi.ajaxOption, activityJson), $.ajax(activityJson), mui("body").on("tap", ".footer-button", function () {
        $("#inputForm").submit()
    })
});